package com.cxs.xiaomi.dao.impl;

import com.cxs.xiaomi.dao.GoodsDao;
import com.cxs.xiaomi.domain.Goods;
import cxs.utils.handler.impl.ResultHandlerAsBeanImpl;
import cxs.utils.handler.impl.ResultHandlerAsListImpl;
import cxs.utils.handler.impl.ResultHandlerAsSingleColumn;
import cxs.utils.templete.JdbcTemplete;

import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/9/14  22:17
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
public class GoodsDaoImpl implements GoodsDao {
    @Override
    public List<Goods> selectByTypeId(int typeId) {
        String sql = "select * from tb_goods where typeid = ?";
        return JdbcTemplete.DQLTemplete(sql, new ResultHandlerAsListImpl<>(Goods.class), typeId);
    }

    @Override
    public Object selectCountsByConditon(String condition, List params) {
        String sql = "select count(*) from tb_goods " + condition;
        return JdbcTemplete.DQLTemplete(sql, new ResultHandlerAsSingleColumn(), params.toArray());
    }

    @Override
    public List<Goods> selectByCondition(String condition, List list) {
        String sql = "select * from tb_goods" + condition;
        return JdbcTemplete.DQLTemplete(sql, new ResultHandlerAsListImpl<>(Goods.class), list.toArray());
    }

    @Override
    public Goods selectById(int id) {
        String sql = "select * from tb_goods where id = ?";
        return JdbcTemplete.DQLTemplete(sql, new ResultHandlerAsBeanImpl<>(Goods.class), id);
    }

    @Override
    public int insert(Goods goods) {
        String sql = "insert into tb_goods values(null,?,?,?,?,?,?,?)";
        Object[] arrs = {
                goods.getName(),
                goods.getPubdate(),
                goods.getPicture(),
                goods.getPrice(),
                goods.getStar(),
                goods.getIntro(),
                goods.getTypeid()
        };
        return JdbcTemplete.DMLTemplete(sql, arrs);
    }

    @Override
    public int deleteById(int id) {
        String sql = "delete from tb_goods where id = ?";
        return JdbcTemplete.DMLTemplete(sql, id);
    }

    @Override
    public int updateById(Goods goods) {
        String sql = "";
        Object[] arrs = {};
        if (goods.getPicture() == null) {
            sql = "update tb_goods set name = ?,pubdate = ?,price = ?,star = ?,intro = ?,typeid = ? where id = ?";
            arrs = new Object[]{
                    goods.getName(),
                    goods.getPubdate(),
                    goods.getPrice(),
                    goods.getStar(),
                    goods.getIntro(),
                    goods.getTypeid(),
                    goods.getId()};
        } else {
            sql = "update tb_goods set name = ?,pubdate = ?,picture = ?,price = ?,star = ?,intro = ?,typeid = ? where id = ?";
            arrs = new Object[]{
                    goods.getName(),
                    goods.getPubdate(),
                    goods.getPicture(),
                    goods.getPrice(),
                    goods.getStar(),
                    goods.getIntro(),
                    goods.getTypeid(),
                    goods.getId()};
        }
        return JdbcTemplete.DMLTemplete(sql, arrs);
    }
}
